/**
 * 复选框全选
 * 
 * @param obj
 *            调用该方法的对象
 * @param chk
 *            复选框的name
 * @param pID
 *            复选框所在父元素id
 * @return
 */
function checkBoxSelectAll(obj, chk, pID) {
	if (chk == null) {
		chk = 'checkboxes';
	}

	var elems = document.getElementById(pID).getElementsByTagName("INPUT");

	for ( var i = 0; i < elems.length; i++) {
		if (elems[i].name == chk || elems[i].name == chk + "[]") {
			elems[i].checked = obj.checked;
		}
	}
}


/**
 * 批量删除
 * 
 * @param url
 *            删除调用的url
 * @param className
 *            需要选中的checkbox的class名称
 * @return
 */
function batchDel(url, className) {
	var str = "";
	$("." + className).each(function() {
		if ($(this).attr("checked") == "checked") {
			str += $(this).val() + ",";
		}
	})
	if (str == "") {
		alert("请至少选择一条记录");
		return;
	}

	str = str.substring(0, str.length - 1);

	if (window.confirm('确定删除选中的记录吗?')) {
		if (url.indexOf("?") >= 0) {
			location.href = url + "&ids=" + str;
		} else {
			location.href = url + "?ids=" + str;
		}
	}
}

/**
 * iframe方式上传图片
 * 
 * @param objID
 * @return
 */
function changeSrc(objID,formID,PathID,SrcID,Dir,NeedDate){
    if(PathID == '')
        PathID = 'hidImgPath';
    if(SrcID == '')
        SrcID = 'imgContent';
    if(Dir == '')
        Dir = 'images';
    if(typeof(NeedDate) == 'undefined' ){
        NeedDate = 0;
    }else{
        NeedDate = 1;
    }
	var name=document.getElementById(objID).value;
	var type=(name.substring(name.lastIndexOf(".")+1,name.length)).toLowerCase();
	if(type!="gif"&&type!="jpeg"&&type!="jpg"&&type!="png")
	{
	    alert("上传文件格式不正确，只支持jpg/jpeg,gif,png文件");
	    return;
	}
	if(window.addEventListener)
	{
		//IE9下有问题
	//    var size=document.getElementById(objID).files[0].fileSize;
	//    if(size>4194304)
	//    {
	//        alert("上传的图片大小超出限制，最大可上传4MB");
	//        return;
	//    }
	}else
	{  
	     var tValue=document.getElementById(objID).value;
	     var img = new Image();  
	     img.src = tValue;
	     img.style.display='none';  
	     document.body.appendChild(img);  
	     setTimeout(function()
	     {
	          document.body.removeChild(img);
	     },1000);
	    
	     if (img.readyState == "complete")
	     {
	         var size=img.fileSize;
	         if(size>4194304)
	         {
		        alert("您所上传的图片大小超出限制，最大可上传4MB");
		        return;
	         }
	     }
	
	}
	isUploadImg = true;
	$('#frameDiv').html('<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>');
	$('#'+formID).attr('target','upload_target');
	$('#'+formID).attr('action','/admin/index/uploadImg/?ElementId='+objID+'&pathid='+PathID+'&srcid='+SrcID+'&dir='+Dir+'&needdate='+NeedDate);
	$('#'+formID).submit();
	$('#'+formID).attr('target','');
	$('#'+formID).attr('action','');
	
	isUploadImg=false;
	/*if (!window.addEventListener) { 
		document.getElementById(objID).outerHTML+=''; }
	else
	{
		$("#"+objID).attr("value",'');
	}*/
}

function validFileUpload()
{
	var a = $.formValidator.isOneValid('hidImgPath');
	if(a)
	{
		$("#txt_fileImg_tip").attr("class","onCorrect");
		$("#txt_fileImg_tip").html("图片上传正确");
	}
	
	if($("#hidHotImgPath").length)
	{
		a = $.formValidator.isOneValid('hidHotImgPath');
		if(a)
		{
			$("#file_gift_hotImg_tip").attr("class","onCorrect");
			$("#file_gift_hotImg_tip").html("图片上传正确");
		}
	}
	
	if($("#hidDetailImgPath").length)
	{
		a = $.formValidator.isOneValid('hidDetailImgPath');
		if(a)
		{
			$("#file_gift_detailImg_tip").attr("class","onCorrect");
			$("#file_gift_detailImg_tip").html("图片上传正确");
		}
	}
}

function submitForm(formID)
{
	if($.formValidator.pageIsValid()){
		document.getElementById(formID).submit();
	}
}

function getCommunityInfo()
{
	var s_province = $('#sel_province').val();
	var s_city = $('#sel_city').val();
	var s_area = $('#sel_area').val();
	$("#hidCo").val("");
	$.ajax({
		type : "POST",
		dataType : "text",
		async : false,
		url : _manageDomain+'communityManage/getCommunityByCity',
		data : {
			province : s_province,
			city : s_city,
			area : s_area
		},
		success : function(data) {
			eval(' var clist=' + data);
			var cHtml = "";
			var ctype = _mutiCommunity?"checkbox":"radio";
			var selID = ","+$("#hidSelCommunityID").val()+",";
			for ( var i = 0; i < clist.length; i++) {
				cHtml += "<label for='rdCommunity"+clist[i].community_id+"'>"+clist[i].community_name+"</label>";
				if(ctype=="radio")
				{
					cHtml += "<input type='radio' onclick='selCommunityRd("+clist[i].community_id+")' name='rdCommunity'";
				}
				else{
					cHtml += "<input onclick='selCommunityCb(this,"+clist[i].community_id+",\""+clist[i].community_name+"\")' type='checkbox' name='rdCommunity[]'";
					
					if(selID.indexOf(","+clist[i].community_id+",")>=0)
					{
						cHtml += " checked='checked' ";
					}
				}
				cHtml += " style='margin-right:12px;' id='rdCommunity"+clist[i].community_id+"' value='"+clist[i].community_id+"' />";
			}
			$("#divCommunity").html(cHtml);
		}
	});
}

function selCommunityCb(obj,community_id,community_name)
{
	var type = "add";
	if($(obj).attr("checked")!="checked")
	{
		type = "del";
	}
	var selVal = $("#selCommunityName").html();
	var selValID = $("#hidSelCommunityID").val();
	
	if(type=="add")
	{
		if(selVal)
		{
			var temp = ","+selVal+",";
			
			if(temp.indexOf(","+community_name+",")<0)
			{
				selVal = selVal+","+community_name;
				
				selValID = selValID+","+community_id;
			}
		}
		else{
			selVal = community_name;
			selValID = community_id;
		}
	}
	else{
		if(selVal)
		{
			selVal = ","+selVal+",";
			selValID = ","+selValID+",";
			
			selVal = selVal.replace(","+community_name+"," , ",");
			selValID = selValID.replace(","+community_id+"," , ",");
			
			if(selVal==",")
			{
				selVal = "";
				selValID = "";
				
			}
			else if(selVal.length>2)
			{
				selVal = selVal.substring(1,selVal.length-1);
				selValID = selValID.substring(1,selValID.length-1);
			}
		}
	}
	
	$("#selCommunityName").html(selVal);
	$("#hidSelCommunityID").val(selValID);
}

function selCommunityRd(community_id)
{
	$("#hidCo").val(community_id);
}

function resetPwd(id,type)
{
	if(!window.confirm('确定重置密码吗?'))
	{
		return;
	}
	
	var module = "shop";
	if(arguments.length==3)
	{
		module = "owner";
	}
	
	$.ajax({
	      type: "POST",
		  data:{ "id":id,"isAjax":1},
	      url: _manageDomain+module+"Manage/reset"+type+"Pwd",
	      success: function(data) {
				if(data == -100){
					window.location.href=_manageDomain+"login/show";
				}
				else if(data== -1000)
				{
					alert("没有权限!");
				}
				else if(data==1)
				{
					alert("操作成功!");
				}
				else{
					alert("操作失败!");
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试!");
		  }
	});
}

function setCard(cardTxtID)
{
	if(arguments.length==2)
	{
		var keys=arguments[1];
		if(keys.keyCode!=13)
		{
			return false;
		}
	}

	var cardv = $("#"+cardTxtID).val();

	if(cardv!="")
	{
		if(!(/^\d{1,12}$/.test(cardv)))
		{
			alert("卡号不正确");
			return false;
		}
		
		if(cardv.length<12)
		{
			var l = 12-cardv.length;
			for(i=0;i<l;i++)
			{
				cardv = "0"+cardv;
			}
			
		}
		
		$("#"+cardTxtID).val(cardv);
	}
	
	return true;
}

function searchShopDetail()
{
	if(setCard("txt_card_value"))
	{
		$("#formSearch").submit();
	}
}

function statusChange(id,type,module)
{
	if(type==1)
	{
		type=2;
		if(!window.confirm('确定冻结账户吗?'))
		{
			return;
		}
	}
	else{
		type=1;
	}
	
	$.ajax({
	      type: "POST",
		  data:{ "id":id,"type":type,"isAjax":1},
	      url: _manageDomain+module+"Manage/changeStatus",
	      success: function(data) {
				if(data == -100){
					window.location.href=_manageDomain+"login/show";
				}
				else if(data== -1000)
				{
					alert("没有权限!");
				}
				else if(data==1)
				{
					alert("操作成功!");
					window.location.reload();
				}
				else{
					alert("操作失败!");
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试!");
		  }
	});
}

function initSwfUpload()
{
	var start = document.cookie.indexOf("PHPSESSID=");
	var end = document.cookie.indexOf(";", start); 
	if (end == -1) end = document.cookie.length; 
	var cookie = document.cookie.substring(start+10, end);
	$("#file_upload").uploadify({
		'uploader'     : _manageDomain+'uploadify/uploadify.swf',
		'script'       : _manageDomain+'ownerManage/importFromExcel?act=upload',
		'cancelImg'    : _manageDomain+'uploadify/cancel.png',
		'queueID'        : 'queue',
		'auto'           : true,
		'buttonImg'		 : _manageDomain+'/uploadify/multiupload.png',
        'fileDataName'   : 'Filedata',
        'sizeLimit'      : 102400,  //Byte
        'fileDesc'       : 'Excel Files',
        'fileExt'        : '*.xls;*.xlsx;',		        
		'multi'          : false,
		'wmode'			 : 'transparent',
		'scriptData'	 :{'PHPSESSID': cookie},
		onComplete: function (evt, queueID, fileObj, response, data) {
			var a=response.split(',');
			
			if(a.length==2)
			{
				var type = a[0];
				if(type==1)
				{
					$("#hid_file_path").val(a[1]);
				}
				else{
					alert("文件上传失败:"+response);
				}
			}
			else
			{
				alert('图片上传失败:'+response);
			}
		},
		onError:function(event,queueID,fileObj,errorObj){
			alert('发生错误:'+errorObj.type+''+errorObj.info);
		}
	});
}
